﻿using Logistics.Model;
using Logistics_Dao;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DapperExtensions;
using System.Data;

namespace Logistics.Service
{
    public class ContractService
    {
        private ContractDao dao = new ContractDao();
        private CompanyDao comDao = new CompanyDao();
        private WuliuCompanyDao wlComDao = new WuliuCompanyDao();
        private WuliuFeeDao feeDao = new WuliuFeeDao();

        public PageData<wuliu_contract_base> GetList(IPredicate predicate, int page, int pageSize)
        {
            return dao.GetList(predicate, page, pageSize);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public PageData<DataTable> GetContractList(IPredicate predicate, int page, int pageSize)
        {
            return dao.GetContractList(predicate, page, pageSize);
        }
        /// <summary>
        ///签约管理新增
        /// </summary>
        /// <param name="apply"></param>
        public void Add(wuliu_company_apply apply)
        {

            var company = comDao.GetCompanyById((int)apply.company_id);
            var wlCompany = wlComDao.Get((int)apply.wuliu_id);
            // var wlFee = feeDao.GetByWuliuNo(apply.wuliu_no);
            var contract = new wuliu_contract_base();
            contract.apply_id = apply.id;
            contract.apply_flag = "10";
            contract.company_id = apply.company_id;
            contract.company_no = company.company_no;
            contract.black_flag = company.black_flag;
            contract.company_name = apply.company_name;
            contract.company_flag = company.signed_status;
            contract.company_city_name = company.city_name;
            contract.company_city_id = company.city_id;
            if (company.city_id_x != null)
            {
                var cityid = company.city_id_x.Split('|');
                if (cityid.Count() > 0)
                {
                    if (cityid.Length == 1)
                    {
                        if (cityid[0] != null)
                            contract.company_city_id3 = Convert.ToInt32(cityid[0]);
                    }
                    if (cityid.Length == 2)
                    {
                        if (cityid[0] != null)
                            contract.company_city_id1 = Convert.ToInt32(cityid[0]);
                        if (cityid[1] != null && cityid.Length > 1)
                            contract.company_city_id2 = Convert.ToInt32(cityid[1]);
                    }
                    if (cityid.Length == 3)
                    {
                        if (cityid[0] != null)
                            contract.company_city_id1 = Convert.ToInt32(cityid[0]);
                        if (cityid[1] != null)
                            contract.company_city_id2 = Convert.ToInt32(cityid[1]);
                        if (cityid[2] != null)
                            contract.company_city_id3 = Convert.ToInt32(cityid[2]);
                    }
                    if(cityid.Length==4)
                    {
                        if (cityid[0] != null)
                            contract.company_city_id1 = Convert.ToInt32(cityid[0]);
                        if (cityid[1] != null)
                            contract.company_city_id2 = Convert.ToInt32(cityid[1]);
                        if (cityid[2] != null)
                            contract.company_city_id3 = Convert.ToInt32(cityid[2]);
                        if (cityid[3] != null)
                            contract.company_city_id4 = Convert.ToInt32(cityid[3]);
                    }
                }
            }
            if (wlCompany.city_id_x != null)
            {
                if (!string.IsNullOrEmpty(wlCompany.city_id_x))
                {
                    var wuliucityid = wlCompany.city_id_x.Split('|');
                    if (wuliucityid.Count() > 0)
                    {
                        if (wuliucityid.Length == 1)
                        {
                            if (wuliucityid[0] != null)
                                contract.wuliu_city_id3 = Convert.ToInt32(wuliucityid[0]);
                        }
                        if (wuliucityid.Length == 2)
                        {
                            if (wuliucityid[0] != null)
                                contract.wuliu_city_id1 = Convert.ToInt32(wuliucityid[0]);
                            if (wuliucityid[1] != null)
                                contract.wuliu_city_id2 = Convert.ToInt32(wuliucityid[1]);
                        }
                        if (wuliucityid.Length == 3)
                        {
                            if (wuliucityid[0] != null)
                                contract.wuliu_city_id1 = Convert.ToInt32(wuliucityid[0]);
                            if (wuliucityid[1] != null)
                                contract.wuliu_city_id2 = Convert.ToInt32(wuliucityid[1]);
                            if (wuliucityid[2] != null)
                                contract.wuliu_city_id3 = Convert.ToInt32(wuliucityid[2]);
                        }
                        if (wuliucityid.Length == 4)
                        {
                            if (wuliucityid[0] != null)
                                contract.wuliu_city_id1 = Convert.ToInt32(wuliucityid[0]);
                            if (wuliucityid[1] != null)
                                contract.wuliu_city_id2 = Convert.ToInt32(wuliucityid[1]);
                            if (wuliucityid[2] != null)
                                contract.wuliu_city_id3 = Convert.ToInt32(wuliucityid[2]);
                            if (wuliucityid[2] != null)
                                contract.wuliu_city_id4 = Convert.ToInt32(wuliucityid[3]);
                        }
                    }
                }
            }
            contract.contract_flag = "1";
            contract.contract_state = "10";
            contract.contract_time = DateTime.Now;
            contract.empty_price = company.empty_price;
            contract.fee = 0;
            contract.Operator = apply.Operator;
            contract.tax = wlCompany.tax;
            contract.white_flag = company.white_flag;
            contract.wuliu_flag = wlCompany.signed_status;
            contract.wuliu_id = wlCompany.wuliu_id;
            contract.wuliu_name = wlCompany.wuliu_name;
            contract.wuliu_city_name = wlCompany.city_name;
            contract.wuliu_no = wlCompany.wuliu_no;
            dao.Add(contract);


        }

        public bool IsExitNum(wuliu_contract_base contract)
        {
            return dao.IsExitNum(contract) == 0;
        }

        /// <summary>
        /// 解约
        /// </summary>
        /// <param name="contranctId"></param>
        /// <returns></returns>
        public bool CancelContract(int contranctId)
        {
            return dao.CancelContract(contranctId);
        }

        public wuliu_contract_base GetContract(int id)
        {
            return dao.GetContract(id);
        }
    }
}
